<?php

/**
 * @package Framework03
 * @author Walter Tamboer
 */

/** 
 * An Access Control List Role.
 */
final class AclRole
{
    /**
     * Allow
     */ 
    const ALLOW = 0x1; 
    /** 
     * Deny 
     */ 
    const DENY = 0x2; 

    /**
     * The name of the role.
     * @var string
     */
    public $Name;
    /**
     * The parent role.
     * @var AclRole
     */
    public $Parent;
    /**
     * A list with all permissions which are allowed or denied.
     * @var array
     */ 
    private $PermissionList;
    /** 
     * Initializes a new instance of this class.
     * @param string $Name The name of the role.
     * @param array $PermissionList The list with all permissions which are allowed or denied.
     */
    public function __construct($Name, array $PermissionList = null)
    {
        $this->Name = $Name;
        $this->PermissionList = ($PermissionList == null) ? array() : $PermissionList;
    }
    /** 
     * Allow a permission for this role
     * @param string $Permission The permission name
     */ 
    public function Allow($Permission) 
    { 
        $Permission = strtolower($Permission); 

        $this->PermissionList[$Permission] = AclRole::ALLOW; 
    } 
    /** 
     * Deny a permission for this role 
     * @param string $Permission The permission name 
     */ 
    public function Deny($Permission) 
    { 
        $Permission = strtolower($Permission); 
         
        $this->PermissionList[$Permission] = AclRole::DENY; 
    } 
    /** 
     * Is a permission allowed? 
     * @param string $Permission The permission name 
     */ 
    public function IsAllowed($Permission) 
    { 
        $Permission = strtolower($Permission); 
         
        if (isset($this->PermissionList[$Permission])) 
            return $this->PermissionList[$Permission] == AclRole::ALLOW; 
        else if ($this->Parent) 
            return $this->Parent->IsAllowed($Permission); 
             
        return false; 
    } 
} 

?>